package com.ctgu.tengyun.service.product.impl;

import com.ctgu.tengyun.dao.mybatis.ProductInfoMapper;
import com.ctgu.tengyun.domain.ProductInfo;
import com.ctgu.tengyun.domain.ProductInfoExample;
import com.ctgu.tengyun.service.product.ProductInfoService;
import com.ctgu.tengyun.util.CollectionsUtil;
import com.ctgu.tengyun.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * Created by liaoweizhi on 2019/7/21.
 */
@Service
public class ProductInfoServiceImpl implements ProductInfoService {

    @Resource
    private ProductInfoMapper mapper;

    @Override
    public ProductInfo selectByPrimaryKey(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ProductInfo> selectByIds(Collection<Integer> ids) {
        if(CollectionUtils.isEmpty(ids)){
            return Collections.emptyList();
        }

        ProductInfoExample example = new ProductInfoExample();
        ProductInfoExample.Criteria criteria = example.createCriteria();

        criteria.andIdIn(new ArrayList<>(ids));
        return mapper.selectByExample(example);
    }

    @Override
    public ProductInfo selectByProductNo(String productNo) {
        ProductInfoExample example = new ProductInfoExample();
        ProductInfoExample.Criteria criteria = example.createCriteria();

        criteria.andProductNoEqualTo(productNo);

        List<ProductInfo> productInfoList = mapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(productInfoList)){
            return productInfoList.get(0);
        }

        return null;
    }

    @Override
    public List<ProductInfo> selectByProductNos(Collection<String> productNos) {
        if(CollectionsUtil.isNullOrEmpty(productNos)){
            return Collections.emptyList();
        }
        ProductInfoExample example = new ProductInfoExample();
        ProductInfoExample.Criteria criteria = example.createCriteria();

        criteria.andProductNoIn(new ArrayList<>(productNos));

        return mapper.selectByExample(example);
    }

    @Override
    public void insertSelective(ProductInfo productInfo) {
        mapper.insertSelective(productInfo);
    }

    @Override
    public Integer countProduct(String productNo, String productName) {
        ProductInfoExample example = new ProductInfoExample();
        ProductInfoExample.Criteria criteria = example.createCriteria();

        criteria.andIsValidEqualTo(1);
        criteria.andProductStatusEqualTo(1); //正常状态
        if(StringUtils.isNotEmpty(productNo)){
            criteria.andProductNoEqualTo(productNo);
        }

        if(StringUtils.isNotEmpty(productName)){
            criteria.andNameLike("%" + productName +"%");
        }

        return mapper.countByExample(example);
    }

    @Override
    public List<ProductInfo> selectProduct(Integer pageNo, Integer pageSize, String productNo, String productName) {
        ProductInfoExample example = new ProductInfoExample();
        ProductInfoExample.Criteria criteria = example.createCriteria();

        criteria.andIsValidEqualTo(1);
        if(!StringUtils.isEmpty(productNo)){
            criteria.andProductNoEqualTo(productNo);
        }

        if(!StringUtils.isEmpty(productName)){
            criteria.andNameLike("%" + productName +"%");
        }

        example.setStartIndex((pageNo - 1) * pageSize);
        example.setPageSize(pageSize);

        return mapper.selectByExample(example);
    }

    @Override
    public int updateByPrimaryKeySelective(ProductInfo record) {
        return mapper.updateByPrimaryKeySelective(record);
    }
}
